<template>
  <div>
    <AplidForm ref="formRef" v-bind="OtherFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { OtherFormConfig } from '../../config'
import { useCommonStore } from '@/stores/common'

const { tempNowObj } = storeToRefs(useCommonStore())

const emit = defineEmits(['refresh'])
const formModel = ref({})
const formRef = ref(null)
const onSubmit = (form, done) => {
  const dpServiceOrgProductPOS =
    form.ServiceOrgProduct &&
    form.ServiceOrgProduct.map((item) => {
      return {
        ...item,
        organizationName: tempNowObj.value.organizationName,
        creditCode: tempNowObj.value.creditCode,
        code: 3018,
      }
    })
  const dpServiceOrgDevicePOS =
    form.ServiceOrgDevice &&
    form.ServiceOrgDevice.map((item) => {
      return {
        ...item,
        organizationName: tempNowObj.value.organizationName,
        creditCode: tempNowObj.value.creditCode,
        code: 3014,
      }
    })
  const params = {
    dpServiceOrgOtherPO: {
      ...form,
      ServiceOrgProduct: undefined,
      code: 3017,
      organizationName: tempNowObj.value.organizationName,
      creditCode: tempNowObj.value.creditCode,
    },
    dpServiceOrgProductPOS,
    dpServiceOrgDevicePOS,
  }
  apiMod
    .view_dpServiceOrgOther_gatherAll(params)
    .then((res) => {
      emit('refresh', res.data)
      done()
    })
    .catch(() => {
      done()
    })
}

const onSave = () => {
  formRef.value!.onSubmit()
}
defineExpose({
  onSave,
})
</script>

<style scoped></style>
